from openerp import models, fields


class product_bom(models.Model):
    _name = 'product.bom'
    _description = u'产品BOM'

    @api.depends('qty', 'price_unit')
    def _compute_amount(self):
        for line in self:
            price = line.price_unit * line.qty
            line.update({
                'price_subtotal': price,
            })

    name = fields.Char(string=u'版本编号', index=True, )
    partner_id = fields.Many2one('res.partner', u'客户', select=True)
    sequence = fields.Integer(string=u"序号", readonly=True)
    parent_left = fields.Integer(string=u"左节点", select=1)
    parent_right = fields.Integer(string=u"右节点", select=1)
    product_id = fields.Many2one('product.product', u'产品子件', select=True)
    uom_id = fields.Many2one(related='product_id.uom_id', store=True, string=u'单位', readonly=True)
    qty = fields.Float(string=u"标准用量", defaults=1)
    loss_rate = fields.Float(string=u"损耗率%")
    price_unit = fields.Float(string=u"单价")
    price_subtotal = fields.Float(compute='_compute_amount', string=u'小计', readonly=True, store=True)
    company_id = fields.Many2one('res.company', string=u'公司')
    parent_id = fields.Many2one('product.bom', string=u'上级目录')
    child_id = fields.One2many('product.bom', 'parent_id', string=u"下级目录")
    is_template = fields.Boolean(u"是否模板", default=False)
